/*
Copyright (c) [2019年5月1日] [吴超]
[MBT_studio] is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
		 http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
*/
#include "stdafx.h"
#include "结构数据.h"

#include <mutex>


typedef struct {
	void*	ptr_userData;
	uint32	count;
	uint32  offset;
	uint32	elt_size;
	uint64	elt_capacity;

	fn_array_free call_free;
}S_RealArray;


#define DEF_数组本体(_A) ((S_RealArray*)_A)


S_ListItem* f_core_createListItem() {
	S_ListItem* item = (S_ListItem*)malloc(sizeof(S_ListItem));
	(*item) = {};
	return item;
}

S_List* f_core_createList() {
	S_List* list = (S_List*)malloc(sizeof(S_List));
	(*list) = {};
	return list;
}

void f_core_releaseList(S_List* list) {
	S_ListItem* item = list->ptr_item;
	for ( ; item; item = item->ptr_尾) {
		if(item->ptr_头) free(item->ptr_头);
	}
	free(list);
}

void f_core_list_clear(S_List* list) {
	S_ListItem* item = list->ptr_item;
	for ( ; item; item = item->ptr_尾) {
		if(item->ptr_头) free(item->ptr_头);
	}
	list->ptr_item = nullptr;
	list->count = 0;
}

S_List* f_core_copyList(S_List* 源) {
	S_List* 新链表 = f_core_createList();

	S_ListItem* item = 源->ptr_item;
	for (S_ListItem* list = item; list; list = list->ptr_尾) {
		f_core_addListEndItem(新链表, list->ptr_userData);
	}

	return 新链表;
}

void f_core_list_free_data(S_List* list, fn_list_data func) {
	S_ListItem* item = list->ptr_item;
	for (; item->ptr_尾; item = item->ptr_尾) {
		func(item->ptr_userData);
	}
}

void f_core_list_sort(S_List* list, fn_list_sort func) {
	uint64 count = f_core_list_size(list);

	S_ListItem* L元素 = list->ptr_item;

	uint32 i = 0;
	for (uint32 i = 0; i < count; ++i) {

		bool 没有交换 = true;
		DEF_FOR_LIST(R元素, L元素->ptr_尾) {
			if (func(L元素->ptr_userData, R元素->ptr_userData)) {
				f_core_list_swapItem(L元素, R元素);
				没有交换 = false;
			}
			else {
				//不是初始元素了 -> point->prt_尾
				L元素 = R元素;
			}
		}

		DEF_FOR_LIST_反向(R元素, L元素) L元素 = R元素;
		
		if (没有交换) break;
	}

	list->ptr_item = L元素;
}

void f_core_list_foreach(S_List* list, fn_list_data func) {
	S_ListItem* item = list->ptr_item;
	DEF_FOR_LIST(元素, item) {
		func(元素->ptr_userData);
	}
}

S_ListItem* f_core_list_last(S_ListItem* item) {
	for (; item->ptr_尾; item = item->ptr_尾);
	return item;
}

S_ListItem* f_core_list_nth(S_ListItem* item, int64 pos) {
	while ((pos> 0) && item) {
		item = item->ptr_尾;
		--pos;
	}
	if(pos) return nullptr;
	return item;
}

/*S_List* f_core_list_offset(S_List* list, int32 offset) {

}*/

uint64 f_core_list_size(S_List* list) {
	uint64 num = 0;
	//for (; list; list = list->ptr_尾, ++num);
	return list->count;
}


S_List* f_core_addListEndItem(S_List* list, void* data) {
	S_ListItem* item = list->ptr_item;
	if (item) {
		for (; item->ptr_尾; item = item->ptr_尾);
		item->ptr_尾 = f_core_createListItem();
		item->ptr_尾->ptr_userData = data;
		item->ptr_尾->ptr_头 = item;
	}
	else {
		list->ptr_item = f_core_createListItem();
		list->ptr_item->ptr_userData = data;
	}
	
	++list->count;
	return list;
}

void f_core_remListItem(S_List* list) {

}

void f_core_list_erase(S_List* list, S_ListItem* item, fn_list_data func) {
	if (item->ptr_头) {
		if (item->ptr_尾) {
			item->ptr_尾->ptr_头 = item->ptr_头;
		}
		else {
			item->ptr_尾->ptr_头 = nullptr;
		}
	}

	if (item->ptr_尾) {
		if (item->ptr_头) {
			item->ptr_头->ptr_尾 = item->ptr_尾;
		}
		else {
			item->ptr_尾->ptr_尾 = nullptr;
		}
	}

	if (func) func(item->ptr_userData);
	free(item);

	--list->count;
}

S_ListItem* f_core_getItem(S_List* list, uint64 id) {
	S_ListItem* item = list->ptr_item;
	for (uint64 i = 0; i < id && item; ++id, item = item->ptr_尾);
	return item;
}

void f_core_list_moveItem(S_List* list, uint64 loc, uint64 d){
	S_ListItem* item = list->ptr_item;

	uint64 offset = 0;
	auto p = item;
	for (; p; p = p->ptr_尾) {
		if (offset >= loc) {
			break;
		}
		++offset;
	}

	offset = 0;
	auto dp = item;
	for (; dp; dp = dp->ptr_尾) {
		if (offset >= d) {
			break;
		}
		++offset;
	}


	S_ListItem* 头_ = p->ptr_头;
	S_ListItem* _尾 = p->ptr_尾;
	if (头_) {
		头_->ptr_尾 = _尾;
	}
	if (_尾) {
		_尾->ptr_头 = 头_;
	}


	_尾 = dp->ptr_尾;
	if (_尾) {
		_尾->ptr_头 = p;
		dp->ptr_尾 = p;

		p->ptr_尾 = _尾->ptr_头;
	}
	else {
		p->ptr_尾 = nullptr;
	}
	p->ptr_头 = dp;
	dp->ptr_尾 = p;

}

void f_core_list_swap(S_List* list_s, S_List* list_e) {
	S_ListItem* temp = list_s->ptr_item;

	list_s->ptr_item = list_e->ptr_item;
	list_e->ptr_item = temp;

	/*if (list_s->ptr_头) {
		list_s->ptr_头->ptr_尾 = list_e;
		list_e->ptr_头 = list_s->ptr_头;
	}
	else {
		list_e->ptr_头 = nullptr;
	}

	if (list_e->ptr_尾) {
		list_e->ptr_尾->ptr_头 = list_s;
		list_s->ptr_尾 = list_e->ptr_尾;
	}
	else {
		list_s->ptr_尾 = nullptr;
	}

	list_s->ptr_头 = list_e;
	list_e->ptr_尾 = list_s;*/
}

void f_core_list_swapItem(S_ListItem* list_s, S_ListItem* list_e) {
	if (list_s->ptr_头) {
		list_s->ptr_头->ptr_尾 = list_e;
		list_e->ptr_头 = list_s->ptr_头;
	}
	else {
		list_e->ptr_头 = nullptr;
	}

	if (list_e->ptr_尾) {
		list_e->ptr_尾->ptr_头 = list_s;
		list_s->ptr_尾 = list_e->ptr_尾;
	}
	else {
		list_s->ptr_尾 = nullptr;
	}

	list_s->ptr_头 = list_e;
	list_e->ptr_尾 = list_s;
}





S_Tree* f_core_createTree(void* data) {
	S_Tree* tree = (S_Tree*)malloc(sizeof(S_Tree));
	(*tree) = { data , f_core_createList() };
	return tree;
}

void f_core_releaseTree(S_Tree* tree) {
	/*if (tree->prt_tree) {
		f_core_releaseTree((S_Tree*)(tree->prt_tree->ptr_userData));
		for (S_List* list = tree->prt_tree; list; list = list->ptr_尾) {

		}
		f_core_releaseList(tree->prt_tree);
	}*/
}

void f_core_TreeSetItem(S_Tree* tree, void* data) {
	tree->ptr_userData = data;
}

void f_core_TreeAddItem(S_Tree* tree, S_Tree* lefTree) {
	f_core_addListEndItem(tree->prt_tree, lefTree);
}

bool f_core_TreeRemoveItem(S_Tree* tree, S_Tree* leaf) {
	/*if (tree == leaf) {
		free(leaf);
		return true;
	}
	else {
	for (S_List* list = leaf->prt_tree; list; list = list->ptr_尾) {
		if (f_core_TreeRemoveItem((S_Tree*)list->ptr_userData, leaf)) break;
	}
	}*/
	return true;
}







#define array_elt_size(array, i) ( (uint64)((S_RealArray*)array)->elt_size )
#define array_elt_len(array, i) ( (uint64)((S_RealArray*)array)->elt_size * (i) )
#define array_elt_pos(array, i) ( (array)->ptr_userData[ array_elt_len( (array), (i) ) ] )

#include <map>
static std::map<S_RealArray*, uint32> DBUGE_记录;
static std::mutex 记录锁;

S_Array* f_core_array_new(uint64 num, uint32 elt_size, bool 多维) {
	S_RealArray* array = (S_RealArray*)malloc(sizeof(S_RealArray));

	array->count = num;
	array->offset = 0;
	array->elt_size = elt_size;
	array->elt_capacity = DEF_Max(1, num) * elt_size;
	//array->ptr_userData = (uint8*)malloc(array->elt_capacity);
	array->ptr_userData = (uint8*)calloc(array->elt_capacity, 1);

	if (多维) {
		array->call_free = f_core_array_free;

		//for (uint32 i = 0; i < num; ++i) {
		//	((S_Array**)array->ptr_userData)[i] = f_core_array_new(0, sizeof(S_Array*));
		//}
	}
	else {
		array->call_free = nullptr;
	}

	//记录锁.lock();
	//static uint32 计数 = 0;
	//DBUGE_记录[array] = ++计数;
	//记录锁.unlock();

	return (S_Array*)array;
}

void f_core_array_free(S_Array* array) {
	#define 数组本体 ((S_RealArray*)array)
	//S_RealArray* a = (S_RealArray*)array;
	if (数组本体->call_free) {
		for (uint32 i = 0; i < 数组本体->count; ++i) {
			S_Array* a1 = (((S_Array**)数组本体->ptr_userData)[i]);
			数组本体->call_free(a1);
		}
	}

	//记录锁.lock();
	//DBUGE_记录.erase(数组本体);
	//记录锁.unlock();

	free(数组本体->ptr_userData);
	free(array);
}

void f_core_array_resize(S_Array* array, uint64 num) {
	//static std::mutex g分配内存锁;
	//g分配内存锁.lock();
	uint32 原数量 = array->count;
	array->count = num;

#define 数组本体 ((S_RealArray*)array)

	uint64 扩充大小 = ((S_RealArray*)array)->elt_size * DEF_Max(1, num);
	if (扩充大小 > ((S_RealArray*)array)->elt_capacity) {
		扩充大小 *= 1.32; 
	}
	else if (扩充大小 < ((S_RealArray*)array)->elt_capacity * 0.7) {
		扩充大小 = ((S_RealArray*)array)->elt_capacity * 0.7;

		if (数组本体->call_free) {
			for (uint32 i = num; i < 原数量; ++i) {
				数组本体->call_free(((S_Array**)数组本体->ptr_userData)[i]);
			}
		}
	}
	else {
		if (数组本体->call_free) {
			for (uint32 i = 原数量; i < num; ++i) {
				((S_Array**)数组本体->ptr_userData)[i] = nullptr;
			}
		}
		return;
	}

	//((S_RealArray*)array)->elt_capacity = DEF_Max(1, num) * ((S_RealArray*)array)->elt_size;
	((S_RealArray*)array)->elt_capacity = 扩充大小;
	array->ptr_userData = (uint8*)realloc((array->ptr_userData), ((S_RealArray*)array)->elt_capacity);


	if (数组本体->call_free) {
		for (uint32 i = 原数量; i < num; ++i) {
			//((S_Array**)数组本体->ptr_userData)[i] = f_core_array_new(0, sizeof(S_Array*));
			((S_Array**)数组本体->ptr_userData)[i] = nullptr;
		}
	}
}

void f_core_array_reserve(S_Array* array, uint64 num) {
	f_core_array_resize(array, num);
	array->count = 0;
}

void f_core_array_push_back(S_Array* array, const uint8* data) {
	uint32 start_index = array->count;
	uint32 元素大小 = ((S_RealArray*)array)->elt_size;
	f_core_array_resize(array, array->count + 1);
	memcpy(&(array->ptr_userData[start_index * 元素大小]), data, 元素大小);
}

void f_core_array_push_back_block(S_Array* array, const uint8* data, uint64 字节数, uint32 元素大小) {
	uint32 start_index = array->count;
	((S_RealArray*)array)->elt_size = 元素大小;
	f_core_array_resize(array, array->count + 字节数);
	memcpy(&(array->ptr_userData[start_index * 元素大小]), data, 字节数 * 元素大小);
}

void f_core_array_pop_back(S_Array* array) {
	if (array->count > 0) --array->count;
}

void f_core_array_pop_front(S_Array* array) {
	uint32 num = array->count - 1;
	uint32 元素大小 = ((S_RealArray*)array)->elt_size;

	for (uint32 i = 0; i < num; ++i) {
		memcpy(&(array->ptr_userData[i * 元素大小]), &(array->ptr_userData[(i + 1) * 元素大小]), 元素大小);
	}

	--array->count;
}

void f_core_array_insert_elt(S_Array* array, uint64 offset, const uint8* data) {
	uint32 元素大小 = ((S_RealArray*)array)->elt_size;
	uint32 start_index = array->count;
	f_core_array_resize(array, array->count + 1);

	if (array->count > 1) {
		for (uint32 i = array->count - 1; i > offset; --i) {
			memcpy(&(array->ptr_userData[i * 元素大小]), &(array->ptr_userData[(i - 1) * 元素大小]), 元素大小);
		}
	}

	memcpy(&(array->ptr_userData[offset * 元素大小]), data, 元素大小);
}

bool f_core_array_erase(S_Array* array, uint32 s, uint32 num) {
	//if (s >= array->count) s = array->count - 1;
	if (s >= array->count) return false;
	if(!array || s+num > array->count) return false;

	uint32 移动数量 = array->count - s - num;
	memmove(&array_elt_pos(array, s), &array_elt_pos(array, s + num), array_elt_len(array, 移动数量));
	array->count -= num;

	return true;
}

void f_core_array_clear(S_Array* array) {
	array->count = 0;
}

void f_core_array_sort(S_Array* array, fn_array_sort sort_fn) {
	uint32 元素大小 = ((S_RealArray*)array)->elt_size;
	uint32 num = array->count;
	if (!num) return;

	void* temp = malloc(元素大小);
	for (uint32 k = 0; k < array->count; ++k) {
		bool 完成 = true;
		for (uint32 i = 0; i < array->count - 1; ++i) {
			uint32 offset_s = (i)*元素大小;
			uint32 offset_e = (i + 1) * 元素大小;
			if (sort_fn(&(array->ptr_userData[offset_s]), &(array->ptr_userData[offset_e]))) {
				memcpy(temp, &(array->ptr_userData[offset_e]), 元素大小);
				memcpy(&(array->ptr_userData[offset_e]), &(array->ptr_userData[offset_s]), 元素大小);
				memcpy(&(array->ptr_userData[offset_s]), temp, 元素大小);
				完成 = false;
			}
		}
		if (完成) break;
	}
	free(temp);
}

void f_core_array_change_elt_size(S_Array* array, const uint32 size) {
	if (size != ((S_RealArray*)array)->elt_size) {
		((S_RealArray*)array)->elt_size = size;

		uint32 num = DEF_Max(array->count, 1);
		uint64 capacity = size * num;
		if ((((S_RealArray*)array)->elt_capacity) < capacity){
			//f_core_array_resize(array, num);
		}
	}
}

uint8* f_core_array_at(S_Array* array, uint32 offset) {
	if (offset >= array->count) offset = array->count - 1;
	return &(array_elt_pos(array, offset));
}

uint8* f_core_array_back(S_Array* array){
	if (array->count) {
		return &(array_elt_pos(array, array->count-1));
	}
	return nullptr;
}

/*void* f_core_array_at_a(S_Array* array, uint32 offset) {
	if (offset >= array->count) offset = array->count - 1;
	return &(array_elt_pos(array, offset));
}*/

uint32 f_core_array_get_elt_size(const S_Array* array) {
	return ((S_RealArray*)array)->elt_size;
}

S_Array* f_core_array_from(const S_Array* array) {
	S_Array* newOb = f_core_array_new(array->count, ((S_RealArray*)array)->elt_size);
	
	if (DEF_数组本体(newOb)->call_free) {
		DEF_数组本体(newOb)->call_free = f_core_array_free;
	}

	f_core_array_copy(newOb, array);
	return newOb;
}

void f_core_array_copy(S_Array* dst, const S_Array* src) {
	if(!dst || !src) return;

	f_core_array_resize(dst, src->count);

	if (DEF_数组本体(src)->call_free) {
		((S_RealArray*)dst)->call_free = f_core_array_free;

		for (uint32 i = 0; i < src->count; ++i) {
			if (((S_Array**)dst->ptr_userData)[i]) {
				f_core_array_copy(((S_Array**)dst->ptr_userData)[i], ((S_Array**)src->ptr_userData)[i]);
			}
			else {
				((S_Array**)dst->ptr_userData)[i] = f_core_array_from(((S_Array**)src->ptr_userData)[i]);
			}
		}
	}
	else {
		if (((S_RealArray*)dst)->elt_size != ((S_RealArray*)src)->elt_size) return;
		memcpy(dst->ptr_userData, src->ptr_userData, array_elt_len(src, src->count));
	}
}

void f_core_array_assign(S_Array* dst, const S_Array* src) {
	f_core_array_resize(dst, src->count);
	for (uint32 i = 0; i < src->count; ++i) {
		dst->ptr_userData[i] = src->ptr_userData[i];
	}
}

void f_core_array_append(S_Array* dst, const S_Array* src) {
	if (!dst || !src) return;
	if(!(src->count) && ((S_RealArray*)dst)->elt_size != ((S_RealArray*)src)->elt_size) return;

	uint32 begin = dst->count;
	f_core_array_resize(dst, begin + src->count);
	memcpy(&(dst->ptr_userData[array_elt_len(src, begin)]), src->ptr_userData, array_elt_len(src, src->count));
}

S_Array* f_core_array_merge(const S_Array* m0, const S_Array* m1) {
	assert(((S_RealArray*)m0)->elt_size == ((S_RealArray*)m1)->elt_size);
	S_Array* newOb = f_core_array_new(m0->count + m1->count, ((S_RealArray*)m0)->elt_size);

	memcpy(newOb->ptr_userData, m0->ptr_userData, array_elt_len(m0, m0->count));
	memcpy(&(newOb->ptr_userData[array_elt_len(m0, m0->count)]), m1->ptr_userData, array_elt_len(m1, m1->count));
	return newOb;
}

void f_core_array_insert(S_Array* dst, const S_Array* src, uint64 offset) {
	uint64 start_index = uint64(dst->count);
	uint32 元素大小 = ((S_RealArray*)dst)->elt_size;
	
	if(((S_RealArray*)dst)->elt_size != ((S_RealArray*)src)->elt_size) return;

	f_core_array_resize(dst, dst->count + src->count);
	
	if (offset < start_index) {
		uint64 num = (start_index - offset) * 元素大小;
		uint64 cpOffset = (offset + src->count) * 元素大小;
		for (uint64 i = 0; i < num; ++i) {
			memcpy(&(dst->ptr_userData[offset * 元素大小 + i]), &(dst->ptr_userData[cpOffset + i]), 元素大小);
		}
	}

	memcpy(&(dst->ptr_userData[start_index * 元素大小]), src->ptr_userData, 元素大小 * src->count);
}

int32 f_core_array_find(S_Array* array, const uint8* data) {
	uint32 元素大小 = array_elt_size(array);

	for (uint32 i = 0; i < array->count; ++i) {
		if (!memcmp(&(array->ptr_userData[i * 元素大小]), data, 元素大小)) {
			return i;
		}
	}
	return -1;
}

S_Array* f_core_array_shuffle(S_Array* a, const S_iVec2Array* index, bool copy) {
	uint32 num = DEF_Min(a->count, index->count);

	uint32 elt_size = ((S_RealArray*)a)->elt_size;
	S_Array* newArray = f_core_array_new(num, elt_size);
	for (uint32 i = 0; i < num; ++i) {
		uint32 索引 = index->ptr_userData[i].x;
		if (索引 < num) {
			memcpy(&(newArray->ptr_userData[i * elt_size]), &(a->ptr_userData[索引 * elt_size]), elt_size);
		}
	}

	if (!copy) {
		f_core_array_copy(a, newArray);
		f_core_array_free(newArray);
		return nullptr;
	}
	return newArray;
}

S_I32Array* f_core_array_random_shuffel(uint32 num, int32 种子) {
	S_I32Array* 数组 = (S_I32Array*)f_core_array_new(num, sizeof(int32));

	for (uint32 i = 0; i < num; ++i) {
		数组->ptr_userData[i] = i;
	}

	--num;
	for (uint32 i = 0; i < num; ++i) {
		int32 index_s = 0;
		int32 index_e = 0;

		srand(种子 + 1 + i);
		index_s = rand() % 数组->count;
		srand(种子 + index_s + i + 1);
		index_e = rand() % 数组->count;
		if (index_s == index_e) continue;

		f_core_array_swap((S_Array*)数组, index_s, index_e);
	}

	return 数组;
}

uint32 f_core_array_statistics(S_Array* a, uint8* 元素) {
	uint32 元素大小 = array_elt_size(a);
	uint32 对比元素数量 = 0;

	for (uint32 i = 0; i < a->count; ++i) {
		if (!memcmp(&(a->ptr_userData[i * 元素大小]), 元素, 元素大小)) {
			++对比元素数量;
		}
	}
	return 对比元素数量;
}

void f_core_array_swap(S_Array* a, uint32 s, uint32 e) {
	uint32 elt_size = array_elt_size(a);
	uint8* temp = (uint8*)malloc(elt_size);

	memcpy(temp, &(array_elt_pos(a, e)), elt_size);
	memcpy(&(array_elt_pos(a, e)), &(array_elt_pos(a, s)), elt_size);
	memcpy(&(array_elt_pos(a, s)), temp, elt_size);

	free(temp);
}

S_Array* f_core_array_extract(S_Array* a, const ivec2& 区间, uint32 num, int32 随机种子, bool copy) {
	
	uint32 begin = DEF_Min(区间.x, 区间.y);
	uint32 end = DEF_Max(区间.y, 区间.x) - begin;

	end = DEF_Min(end, a->count);
	num = DEF_Min(end, num);


	uint32 元素大小 = array_elt_size(a);
	S_Array* newArray = f_core_array_new(0, 元素大小);

	if (end) {
		if (copy) {
			S_I32Array* index = f_core_array_random_shuffel(num, 随机种子);
			//index = offset + (rand() % (num));
			for (uint32 i = 0; i < num; ++i) {
				f_core_array_push_back(newArray, f_core_array_at(a, index->ptr_userData[i]));
			}
			f_core_array_free((S_Array*)index);
		}
		else {
			uint32 index = 0;
			for (uint32 i = 0; i < num; ++i) {
				srand(随机种子 + 1 + i);
				uint32 index = begin + (rand() % (end - i));

				index = begin + (rand() % (num - i));
				f_core_array_push_back(newArray, f_core_array_at(a, index));
				f_core_array_erase(a, index);
			}
		}
	}

	return newArray;
}


void f_core_array_save(S_Array* array, FILE* f) {
	fwrite(&(((S_RealArray*)array)->count), sizeof(uint32), 1, f);
	fwrite(&(((S_RealArray*)array)->elt_size), sizeof(uint32), 1, f);
	
	fwrite( (array->ptr_userData), ((S_RealArray*)array)->elt_size, ((S_RealArray*)array)->count, f);
}

void f_core_array_load(S_Array* array, FILE* f) {
	uint32 count;
	fread(&count, sizeof(uint32), 1, f);
	fread(&(((S_RealArray*)array)->elt_size), sizeof(uint32), 1, f);
	
	f_core_array_resize(array, count);
	fread( (array->ptr_userData), ((S_RealArray*)array)->elt_size, ((S_RealArray*)array)->count, f);
}




void f_core_array_包(S_Array* array, S_UI8Array* 块) {
	uint64 字节数 = ((S_RealArray*)array)->count * ((S_RealArray*)array)->elt_size;

	f_core_array_push_back_block((S_Array*)块, (uint8*)&(((S_RealArray*)array)->count), sizeof(uint32));
	f_core_array_push_back_block((S_Array*)块, (uint8*)&(((S_RealArray*)array)->elt_size), sizeof(uint32));
	
	f_core_array_push_back_block((S_Array*)块, array->ptr_userData, 字节数);
}

const uint8* f_core_array_解(S_Array* array, const uint8* 块) {
	((S_RealArray*)array)->elt_size = ((uint32*)块)[1];

	f_core_array_resize(array, ((uint32*)块)[0]);

	uint64 字节数 = ((S_RealArray*)array)->count * ((S_RealArray*)array)->elt_size;
	块 = &块[sizeof(uint32) * 2];
	memcpy(array->ptr_userData, 块, 字节数);

	块 = &块[字节数];
	return 块;
}


S_iVec2Array* f_core_index_sort(S_I32Array* a, bool 降序) {
	S_iVec2Array* index = (S_iVec2Array*)f_core_array_new(a->count, sizeof(ivec2));
	
	if (!a->count) return index;
	uint32 num = a->count;

	for (int32 i = 0; i < num; ++i) {
		index->ptr_userData[i] = {i, a->ptr_userData[i]};
	}
	//return index;
	--num;
	for (uint32 i = 0; i < num; ++i) {
		for (uint32 k = 0; k < num; ++k) {
			ivec2 e = index->ptr_userData[k + 1];
			if (降序) {
				if (index->ptr_userData[k].y < e.y) {
					index->ptr_userData[k + 1] = index->ptr_userData[k];
					index->ptr_userData[k] = e;
				}
			}
			else {
				if (index->ptr_userData[k].y > e.y) {
					index->ptr_userData[k + 1] = index->ptr_userData[k];
					index->ptr_userData[k] = e;
				}
			}
		}
	}
	return index;
}



